package resp

import (
	"encoding/json"
	"net/http"
)

var _ To = &tStdResp{}

type tStdResp struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data StdRespData `json:"data"`
}

type StdRespData struct {
	Data  any `json:"data"`
	Other any `json:"other"`
}

func (r *tStdResp) SetCode(code int) {
	r.Code = code
}

func (r *tStdResp) SetMsg(msg string) {
	r.Msg = msg
}

func (r *tStdResp) SetData(data any) {
	r.Data.Data = data
}

func (r *tStdResp) SetOther(other any) {
	r.Data.Other = other
}

func (r *tStdResp) ToJson() []byte {
	marshal, _ := json.Marshal(r)
	return marshal
}

func (r *tStdResp) Value() any {
	return r
}

func (r *tStdResp) Gin() (int, any) {
	return r.Code, r
}

func (r *tStdResp) GinOK() (int, any) {
	return http.StatusOK, r
}

func NewStandard() Resp {
	return new(stdResp)
}

type stdResp byte

func (*stdResp) New(code int, msg string, data any, other any) To {
	return &tStdResp{
		Code: code,
		Msg:  msg,
		Data: StdRespData{
			Data:  data,
			Other: other,
		},
	}
}

func (*stdResp) NewOK(list ...any) To {
	var rt = &tStdResp{Code: http.StatusOK}
	length := len(list)
	switch length {
	case 0:
	case 1:
		rt.Data.Data = list[0]
	case 2:
		rt.Data.Data = list[0]
		rt.Data.Other = list[1]
	default:
		rt.Data.Data = list[0]
		rt.Data.Other = list[1:]
	}
	return rt
}

func (*stdResp) NewErr(code int, msg string) To {
	return &tStdResp{
		Code: code,
		Msg:  msg,
	}
}
